<template>
    <div id="notice" :class="{ 'close-tab-second': isExit3 }">
        <el-tabs type="card" v-model="activeKey" closable @tab-remove="removeTab">
            <el-tab-pane name="待办提醒规则设置">
                <span slot="label">
                    <i class="el-icon-tickets"></i>
                    待办提醒规则设置
                </span>
                <ReminderRulesList v-if="activeKey === '待办提醒规则设置'"></ReminderRulesList>
            </el-tab-pane>
            <el-tab-pane v-for="item in tabGroup" :key="item.name" :label="item.name" :name="item.name">
                <!-- 待办提醒 -->
                <ReminderRulesDetail
                    v-if="['addReminderRules', 'look', 'edit'].includes(item.type)"
                    :data="item"
                    @close-tab="removeTab"
                ></ReminderRulesDetail>
            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import WarningNoticeDetailBlackWhite from '@/pages/system-maintence/warning-notice/warning-notice-detail-black-white.vue';
import WarningNoticeDetailEngine from '@/pages/system-maintence/warning-notice/warning-notice-detail-engine.vue';
import WarningNoticeDetailRiskDeviation from '@/pages/system-maintence/warning-notice/warning-notice-detail-risk-deviation.vue';
import WarningNoticeDetailOIS from '@/pages/system-maintence/warning-notice/warning-notice-detail-OIS.vue';
import EmailSendManage from '@/pages/system-maintence/warning-notice/email-send-manage.vue';
import WarningNoticeList from '@/pages/system-maintence/warning-notice/warning-notice-list.vue';
import WarningNoticeDetail from '@/pages/system-maintence/warning-notice/warning-notice-detail.vue';
import WarningNoticeDetailStaticRiskDisposal
    from '@/pages/system-maintence/warning-notice/warning-notice-detail-static-risk-disposal.vue';
import WarningNoticeDetailStatic from '@/pages/system-maintence/warning-notice/warning-notice-detail-static.vue';
import EmailHistory from '@/pages/system-maintence/warning-notice/email-history.vue';
import { getConfigItem } from '@/utils/tools';
import ReminderRulesList from '@/project/huatai-asset/pages/to-do-rules/reminder-rules-list.vue';
import ReminderRulesDetail from '@/project/huatai-asset/pages/to-do-rules/reminder-rules-detail.vue';

@Component({
    name: 'ReminderRules',
    components: {
        WarningNoticeList,
        WarningNoticeDetail,
        WarningNoticeDetailStatic,
        EmailHistory,
        WarningNoticeDetailBlackWhite,
        WarningNoticeDetailEngine,
        WarningNoticeDetailRiskDeviation,
        WarningNoticeDetailOIS,
        WarningNoticeDetailStaticRiskDisposal,
        EmailSendManage,
        ReminderRulesList,
        ReminderRulesDetail
    },
})
export default class ReminderRules extends Vue {
    private activeKey: string = '待办提醒规则设置';
    private tabGroup: any[] = [];
    isExit3: boolean = false; // 是否存在资管的静态风险处置
    created() {
        this.$on('addTables', this.changComponets);
    }

    mounted() {
        const isExit3: boolean = getConfigItem('WEB_DOCKING') === 'CMSCHINA';
        this.isExit3 = isExit3;
    }

    // 监控子组件发送的命令
    changComponets(datas: any) {
        const data: any = { type: datas.type, content: datas.value };
      if (datas.type === 'addReminderRules') {
            data.name = '新增待办提醒规则';
        } else if (datas.type === 'edit') {
            data.name = '修改待办提醒规则-' + datas.value.cRuleId;
        } else if (datas.type === 'look') {
          data.name = '查看待办提醒规则-' + datas.value.cRuleId;
      }
        const item = this.tabGroup.find((item: any) => item.name === data.name);
        if (!item) {
            this.addTab(data);
        } else {
            this.activeKey = data.name;
        }
    }

    private addTab(targetName: any) {
        this.tabGroup.push(targetName);
        this.activeKey = targetName.name;
    }

    private removeTab(targetName: string) {
        console.log(targetName, 'targetName1');

        let tabs = this.tabGroup;
        let activeName = this.activeKey;
        if (activeName === targetName) {
            tabs.forEach((tab: any, index: any) => {
                if (tab.name === targetName) {
                    let nextTab = tabs[index + 1] || tabs[index - 1];
                    if (nextTab) {
                        activeName = nextTab.name;
                    }
                }
            });
        }
        var str = targetName;
        str = str.substr(0, str.indexOf('：'));
        // 默认跳转
        this.activeKey = '待办提醒规则设置';
        this.tabGroup = tabs.filter((tab: any) => tab.name !== targetName);
    }
}
</script>
<style lang="scss" scoped>
#notice {
    padding-top: 5px;
    min-width: 1200px;
    height: 100%;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item {
    line-height: 34px;
    padding: 0 25px;
}

#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item span {
    border-right: 0px solid #fff;
}
#notice /deep/ .el-tabs .el-tabs__nav .el-tabs__item:nth-child(1) .el-icon-close {
    display: none;
}
.close-tab-second /deep/ .el-tabs--top.el-tabs .el-tabs__nav .el-tabs__item:nth-child(2) .el-icon-close {
    display: none;
}

/deep/ .el-tabs {
    height: 100%;
    .el-tabs__header {
        min-width: auto;
        z-index: 111;
    }
    .el-tabs__content {
        height: calc(100% - 40px);
        .el-tab-pane {
            height: 100%;
        }
    }
}
</style>
